import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class GenerateTestsFiles {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		File file1 = SimpleTest.getFileToOpen(args, 0, "input.tsv", "Choose the input.tsv file");
		BufferedReader brInput1;
		if (file1 == null) {
			return;
		} else {
			try {
				brInput1 = new BufferedReader(new FileReader(file1));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return;
			}
		}
		File file3 = SimpleTest.getFileToOpen(args, 1, "locations1.tsv", "Choose the locations1.tsv file");
		BufferedReader brLocationsConstant;
		if (file3 == null) {
			return;
		} else {
			try {
				brLocationsConstant = new BufferedReader(new FileReader(file3));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return;
			}
		}

		File file4 = SimpleTest.getFileToOpen(args, 2, "locations2.tsv", "Choose the locations2.tsv file");
		BufferedReader brLocationsVariant;
		if (file4 == null) {
			return;
		} else {
			try {
				brLocationsVariant = new BufferedReader(new FileReader(file4));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return;
			}
		}

		try {
			BufferedWriter bwfake = new BufferedWriter(new FileWriter(new File(file1.getAbsolutePath() + ".fake")));
			BufferedWriter bwreal = new BufferedWriter(new FileWriter(new File(file1.getAbsolutePath() + ".real")));
			boolean constantPos = true;

			int locationConstantPos = -1;
			char locationConstantChar = ' ';
			int locationVariantPos = -1;
			char locationVariantChar = ' ';
			float locationVariantPorc = 1000;

			String lineLocationConstant;
			String lineLocationVariant;
			String lineInput;
			int currentPos = 789;
			String[] inputs;
			String[] locations;
			int inputPos = -1;
			char inputChar = ' ';

			lineLocationConstant = brLocationsConstant.readLine();
			locations = lineLocationConstant.split("\t");
			locationConstantPos = Integer.parseInt(locations[0]);
			locationConstantChar = locations[1].charAt(0);

			lineLocationVariant = brLocationsVariant.readLine();
			locations = lineLocationVariant.split("\t");
			locationVariantPos = Integer.parseInt(locations[0]);
			locationVariantChar = locations[1].charAt(0);
			locationVariantPorc = Float.parseFloat(locations[2]);

			while ((lineInput = brInput1.readLine()) != null) {
				lineInput = lineInput.trim();
				inputs = lineInput.split("\t");
				inputPos = Integer.parseInt(inputs[0]);
				inputChar = inputs[1].charAt(0);
				if (currentPos == inputPos) {
					if (constantPos) {
						if (locationConstantChar != inputChar) {
							bwfake.write(lineInput);
							bwfake.newLine();
						}
					} else {
						if (locationVariantChar == inputChar && locationVariantPorc <= 6.5) {
							bwreal.write(lineInput);
							bwreal.newLine();
						}
					}
				} else {
					currentPos = inputPos;
					if (constantPos) {
						// read next location constant
						lineLocationConstant = brLocationsConstant.readLine();
						if (lineLocationConstant == null) {
							locationConstantPos = -1;
							locationConstantChar = ' ';
						} else {
							locations = lineLocationConstant.split("\t");
							locationConstantPos = Integer.parseInt(locations[0]);
							locationConstantChar = locations[1].charAt(0);
						}
						constantPos = (locationConstantPos == inputPos);
					} else {
						// read next location variant
						lineLocationVariant = brLocationsVariant.readLine();
						if (lineLocationVariant == null) {
							locationVariantPos = -1;
							locationVariantChar = ' ';
						} else {
							locations = lineLocationVariant.split("\t");
							locationVariantPos = Integer.parseInt(locations[0]);
							locationVariantChar = locations[1].charAt(0);
							locationVariantPorc = Float.parseFloat(locations[2]);
						}
						constantPos = (locationVariantPos != inputPos);
					}
					if (constantPos) {
						if (locationConstantChar != inputChar) {
							bwfake.write(lineInput);
							bwfake.newLine();
						}
					} else {
						if (locationVariantChar == inputChar && locationVariantPorc <= 6.5) {
							bwreal.write(lineInput);
							bwreal.newLine();
						}
					}
				}
			}
			bwfake.flush();
			bwreal.flush();
			bwfake.close();
			bwreal.close();
			brInput1.close();
			brLocationsConstant.close();
			brLocationsVariant.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
